<?php
/**
 * 单页面文章系统 分类控制器
 **/
class InfoclassAction extends Action
{
    function _initialize()
    {
        
    }
    /**
     * 获取所有的分类
     * */
    public function AdminIndex()
    {
        EasyACL('Infoclass','AdminIndex',$_SESSION);
        $Class = D('Infoclass');
        $count = $Class->count(NULL,'cid');
        $listRows = 15;
        $fields = 'cid,name,created,updated';
        import('ORG.Util.Page');
        $p = new Page($count,$listRows);
        $list = $Class->xfindall(NULL,$fields,'cid desc',$p->firstRow . ',' . $p->listRows);
        $page = $p->show();
        
        //赋值到模板
        $this->assign('Class',$list);
        $this->assign('page',$page);
        $this->display();
    }

    /**
     * 创建分类
     * */
    public function AdminCreate()
    {
        EasyACL('Infoclass','AdminCreate',$_SESSION);
        //POST
        if ($this->isPost()) {
            $Class = D('Infoclass');
            if ($Class->create()) {
                if ($Class->add()) {
                    $this->redirect('AdminIndex','Infoclass');
                } else {
                    return $this->error($Class->getError());
                }
            } else {
                return $this->error($Class->getError());
            }
        }

        $this->display();
    }

    /**
     * 修改分类
     * */
    public function AdminUpdate()
    {
        EasyACL('Infoclass','AdminUpdate',$_SESSION);
        $Class = D('Infoclass');
        
        $Input = Input::getInstance();
        $Input->filter('addslashes');
        
		import('ORG.Util.HashMap');
		$conditions = new HashMap();
        //POST
        if ($this->isPost()) {
        	//获取要修改的分类
        	$conditions->put('cid',array('EQ',$Input->post('cid')));
        	$Class->where($conditions)->find();
        	if ($Class->create()) {
        		if ($Class->save()) {
        			$this->redirect('AdminIndex','Infoclass');
        		} else {
        			return $this->error($Class->getError());
        		}
        	} else {
        		return $this->error($Class->getError());
        	}
        }
		//获取要修改的分类信息
		$conditions->put('cid',array('EQ',$Input->get('cid')));
		$ClassContent = $Class->where($conditions)->find();
		
		//设置到模板
		$this->assign('Class',$ClassContent);
		$this->display();
    }
    
    /**
     * 删除某个分类
     * @param $id Int
     * @return TRUE/FALSE
     */
    public function AdminDel($id)
    {
    	EasyACL('Infoclass','AdminDel',$_SESSION);
    	//如果传入值不为空，并且是数字
    	if (!empty($id) && is_numeric($id)){
    		$Class = D('Infoclass');
    		if ($Class->deleteById($id))
    			return TRUE;
    		return FALSE;
    	}
    	//跳转到列表页面
    	$this->redirect('Infoclass','AdminIndex',$_SESSION);
    }
    
    /**
     * 删除多个
     */
    public function AdminDels()
    {
    	EasyACL('Infoclass','AdminDels',$_SESSION);
    	//POST
    	if ($this->isPost()) {
    		$cid = $_POST['cid'];
    		$Error = '';
    		foreach ($cid as $id) {
    			if (!$this->AdminDel($id)) {
    				$Error .= '尝试删除编号为' . $id . '分类失败!';
    			}
    		}
    		//如果错误信息为空的话
    		if (!empty($Error)) {
    			return $this->error($Error);
    		}
    		return $this->redirect('AdminIndex','Infoclass');
    	}
    	
    	//GET
    	if ($this->isGet()) {
    		$Input = Input::getInstance();
    		$Input->filter('addslashes');
    		if ($this->AdminDel($Input->get('cid'))) {
    			return $this->redirect('AdminIndex','Infoclass');
    		} else {
    			return $this->error('尝试删除分类失败!');
    		}
    	}
    }
}
?>









